/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jack;

import com.jack.CounterItem;
import com.jack.HomeworkMidlet;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.*;

/**
 *
 * @author jack
 */
public class SystemForm extends Form implements CommandListener {

    private RecordStore rs;
    HomeworkMidlet midlet;
    Command exit;
    Command listRecords;
    Command syslogCommand;
    Command showMemoryCommand;
    Command gcCommand;

    public SystemForm(HomeworkMidlet midlet) {
        super("工具");

        this.midlet = midlet;

        exit = new Command("返回", Command.EXIT, 0);
        listRecords = new Command("所有记录", Command.ITEM, 0);
        syslogCommand = new Command("系统日志", Command.ITEM, 1);
        showMemoryCommand = new Command("显示内存", Command.ITEM, 2);
        gcCommand = new Command("回收内存", Command.ITEM, 3);
        addCommand(exit);
        addCommand(listRecords);
        //addCommand(syslogCommand);
        addCommand(showMemoryCommand);
        addCommand(gcCommand);
        setCommandListener(this);
    }

    private void showRecords() {
        try {
            deleteAll();

            RecordEnumeration re = midlet.getRecordEnum();

            while (re.hasNextElement()) {
                try {
                    CounterItem item = new CounterItem(re.nextRecord());
                    append(item.toString() + "\n");
                    midlet.log(item.toString());

                } catch (IOException ex) {
                    midlet.log(ex);
                } catch (RecordStoreNotOpenException ex) {
                    midlet.log(ex);
                } catch (InvalidRecordIDException ex) {
                    midlet.log(ex);
                } catch (RecordStoreException ex) {
                    midlet.log(ex);
                }

            }
        } catch (RecordStoreNotOpenException ex) {
            midlet.log(ex);
        }
    }

    private void showMemory() {
        deleteAll();
        Runtime runtime = Runtime.getRuntime();
        append(new StringItem("Free/Total: ", "" + runtime.freeMemory() + "/" + runtime.totalMemory()));
        append(new StringItem("Used: ", "" + (runtime.totalMemory() - runtime.freeMemory())));

    }

    private void showLog() {
        deleteAll();
        append("功能未实现。");
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exit) {
            midlet.makeCurrent();
        } else if (c == listRecords) {
            showRecords();
        } else if (c == syslogCommand) {
            showLog();
        } else if (c == showMemoryCommand) {
            showMemory();
        } else if (c == gcCommand) {
            Runtime.getRuntime().gc();
            showMemory();
        }
    }

    /**
     * @return the rs
     */
    public RecordStore getRs() {
        return rs;
    }

    /**
     * @param rs the rs to set
     */
    public void setRs(RecordStore rs) {
        this.rs = rs;
    }
}
